

package tank02;

import java.awt.*;

/**
 * 坦克类
 * 属性：x\y坐标\方向\速度\颜色
 * 移动、绘制
 */
public class Tank {

    //方向上
    public static final int DIRECTION_UP = 1;
    //方向下
    public static final int DIRECTION_DOWN = 2;
    //方向左
    public static final int DIRECTION_LEFT = 3;
    //方向右
    public static final int DIRECTION_RIGHT = 4;

    //x坐标
    private int x;
    //y坐标
    private int y;
    //方向
    private int direction;
    //速度
    private int speed = 5;
    //颜色
    private Color color;

    public Tank(int x, int y, Color color) {
        this.x = x;
        this.y = y;
        this.color = color;
    }

    public Tank() {
    }

    /**
     * 移动方法
     */
    public void move(){
        //判断坦克的方向
        switch(direction){
            case DIRECTION_UP:
                y -= speed;  // y = y - speed 向上移动
                break;
            case DIRECTION_DOWN:
                y += speed;  // 向下移动
                break;
            case DIRECTION_LEFT:
                x -= speed;  // 向左移动
                break;
            case DIRECTION_RIGHT:
                x += speed;  // 向右移动
                break;
        }
    }

    /**
     * 绘制坦克
     * @param g
     */
    public void draw(Graphics g){
        //设置颜色
        g.setColor(color);
        //判断坦克的方向
        switch(direction){
            case DIRECTION_UP: //绘制向上的坦克
                //画左履带
                g.fill3DRect(x,y,10,60,false);
                //画车身
                g.fill3DRect(x + 10,y + 5,30,50,false);
                //画右履带
                g.fill3DRect(x + 10 + 30,y,10,60,false);
                //画炮塔
                g.fillOval(x + 10,y + 5 + 10,30,30);
                //画炮管
                g.fill3DRect(x + 10 + 15 - 3,y,6,15,false);
                break;
            case DIRECTION_DOWN:  //绘制向上的坦克
                //画左履带
                g.fill3DRect(x,y,10,60,false);
                //画车身
                g.fill3DRect(x + 10,y + 5,30,50,false);
                //画右履带
                g.fill3DRect(x + 10 + 30,y,10,60,false);
                //画炮塔
                g.fillOval(x + 10,y + 5 + 10,30,30);
                //画炮管
                g.fill3DRect(x + 10 + 15 - 3,y + 15 + 30,6,15,false);
                break;
            case DIRECTION_LEFT:
                //画左履带
                g.fill3DRect(x,y,60,10,false);
                //画车身
                g.fill3DRect(x + 5,y + 10,50,30,false);
                //画右履带
                g.fill3DRect(x ,y + 10 + 30,60,10,false);
                //画炮塔
                g.fillOval(x + 5 + 10,y + 10,30,30);
                //画炮管
                g.fill3DRect(x,y  + 10 + 15 - 3,15,6,false);
                break;
            case DIRECTION_RIGHT:
                //画左履带
                g.fill3DRect(x,y,60,10,false);
                //画车身
                g.fill3DRect(x + 5,y + 10,50,30,false);
                //画右履带
                g.fill3DRect(x ,y + 10 + 30,60,10,false);
                //画炮塔
                g.fillOval(x + 5 + 10,y + 10,30,30);
                //画炮管
                g.fill3DRect(x + 15 + 30,y  + 10 + 15 - 3,15,6,false);
                break;
        }
    }
    //绘制子弹
    if(bullel != null && !bullel.isRemoved()){
        g.filloval(bullel.getX(),bullet.getY(),)
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public int getDirection() {
        return direction;
    }

    public void setDirection(int direction) {
        this.direction = direction;
    }

    public int getSpeed() {
        return speed;
    }

    public void setSpeed(int speed) {
        this.speed = speed;
    }

    /**
     * 子弹类
     */
    public static class Bullet implements Runnable{

        public static final int WIDTH = 6;
        public static final int HEIGHT = 6;

        //x坐标
        private int x;
        //y坐标
        private int y;
        //方向
        private int direction;
        //颜色
        private Color color;
        //速度
        private int speed = 5;
        //是否删除
        private boolean removed;

        public Bullet(int x, int y, int direction) {
            this.x = x;
            this.y = y;
            this.direction = direction;
        }

        public Bullet(int x, int y, int direction, Color color) {
            this(x,y,direction);
            this.color = color;
        }

        public int getX() {
            return x;
        }

        public void setX(int x) {
            this.x = x;
        }

        public int getY() {
            return y;
        }

        public void setY(int y) {
            this.y = y;
        }
        public int getDirection() {
            return direction;
        }

        public void setDirection(int direction) {
            this.direction = direction;
        }

        public Color getColor() {
            return color;
        }

        public void setColor(Color color) {
            this.color = color;
        }

        public int getSpeed() {
            return speed;
        }

        public void setSpeed(int speed) {
            this.speed = speed;
        }

        public boolean isRemoved() {
            return removed;
        }

        public void setRemoved(boolean removed) {
            this.removed = removed;
        }

        @Override
        public void run() {
            while(true){
                switch (direction){
                    case DIRECTION_UP:
                        y -= speed;
                        break;
                    case DIRECTION_DOWN:
                        y += speed;
                        break;
                    case DIRECTION_LEFT:
                        x -= speed;
                        break;
                    case DIRECTION_RIGHT:
                        x += speed;
                        break;
                }
                if(x < 0 || x > 600 || y < 0 || y > 800){
                    removed = true;
                    break;
                }
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}